package com.gotye.particlesystem.objects;

import android.opengl.GLES20;
import android.util.Log;

import com.gotye.particlesystem.data.VertexArray;
import com.gotye.particlesystem.gles.GlesUtilLib;
import com.gotye.particlesystem.programs.TextureShaderProgram;

import java.nio.ByteBuffer;

/**
 * Created by Michael.Ma on 2016/7/1.
 */
public class CommonObject {
    private static final int POSITION_COMPONENT_COUNT = 3;
    private static final int NORMAL_COMPONENT_COUNT = 3;
    private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2;
    private static final int BYTES_PER_FLOAT = 4;

    private VertexArray vertexArray;
    private VertexArray normalArray;
    private VertexArray textureArray;

    private int NumVerts;

    public CommonObject(int index) {
        vertexArray = new VertexArray(GlesUtilLib.getVertexArray(index));
        normalArray = new VertexArray(GlesUtilLib.getNormalArray(index));
        textureArray = new VertexArray(GlesUtilLib.getTextureArray(index));
        NumVerts = GlesUtilLib.getArrayNum(index);
    }

    public void bindData(TextureShaderProgram textureShaderProgram) {
        vertexArray.setVertexAttribPointer(
                0,
                textureShaderProgram.getPositionAttributeLocation(),
                POSITION_COMPONENT_COUNT,
                0);
        normalArray.setVertexAttribPointer(
                0,
                textureShaderProgram.getNormalAttributeLocation(),
                NORMAL_COMPONENT_COUNT,
                0);
        textureArray.setVertexAttribPointer(
                0,
                textureShaderProgram.getTextureCoordinatesAttributeLocation(),
                TEXTURE_COORDINATES_COMPONENT_COUNT,
                0);
    }

    public void draw() {
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, NumVerts);
    }
}